package com.haogu.controller;

import com.haogu.entity.Announcement;
import com.haogu.entity.Department;
import com.haogu.entity.User;
import com.haogu.service.Announcements;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 公告控制器
 */
@Controller
@RequestMapping("/announcement")
public class AnnouncementController {

    @Resource
    private Announcements announcements;

    /**
     * 跳公告页面
     * @return
     */
    @RequestMapping("/toAnnouncement")
    public String toAnnouncement() {
        return "announcement/list_announcement";
    }

    /**
     * 查询所有公告
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/tolist")
    @ResponseBody
    public Map tolist(Integer page, Integer rows,String title,String departmentid) {
        return announcements.selectAnnouncement(page, rows, title, departmentid);
    }

    /**
     * 跳查看公告详情
     *
     * @return
     */
    @RequestMapping("/toselectAnnou")
    public String toSelectAnnou(Integer id, ModelMap modelMap) {
        Announcement announcement = announcements.selectOneAnnous(id);
        modelMap.addAttribute("announcement", announcement);
        return "announcement/show_announcement";
    }

    /**
     * 跳添加页面
     * @return
     */
    @RequestMapping("/toAddAnnou")
    public String toAddAnnou(ModelMap modelMap) {
        List<Department> departments = announcements.selectDepartment();
        modelMap.addAttribute("departments", departments);
        return "announcement/add_announcement";
    }

    /**
     * 添加
     *
     * @param announcement
     * @return
     */
    @RequestMapping("/addAnnouncement")
    @ResponseBody
    public Map addAnnouncement(Announcement announcement, HttpSession session) {
        User user = (User) session.getAttribute("user");
        announcement.setUserId(user.getId());
        Map map = announcements.insertAnnous(announcement);
        return map;
    }

    /**
     * 跳修改页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/toChange")
    public String toChange(Integer id,ModelMap modelMap) {
        List<Department> departments = announcements.selectDepartment();
        modelMap.addAttribute("departments", departments);

        Announcement announcement = announcements.selectOneAnnous(id);
        modelMap.addAttribute("announcement", announcement);
        return "announcement/change_announcement";
    }

    /**
     * 修改
     * @param announcement
     * @return
     */
    @RequestMapping("/changes1")
    @ResponseBody
    public Map changes1(Announcement announcement) {
        int i = announcements.changes1(announcement);
        Map map = new HashMap();

        if (i == 1) {
            map.put("success", true);
            map.put("message", "修改成功！");
        } else {
            map.put("success", false);
            map.put("message", "修改失败！");
        }
        return map;
    }

    @RequestMapping("/toDel")
    @ResponseBody
    public Map toDel(String ids) {
        String[] split = ids.split(",");
        int del = 0;
        for (int i = 0; i < split.length; i++) {
            del = announcements.del(Integer.valueOf(split[i]));
        }
        Map map = new HashMap();
        if (del > 0) {
            map.put("success", true);
            map.put("message", "删除成功!");
        } else {
            map.put("success", false);
            map.put("message", "删除失败!");
        }
        return map;
    }
}
